const { mineApi } = require("../../../../utils/request/api/mine");

Page({
  data: {
    title: "",
    money: "",
    apply: {
      order_id: 0, //订单ID
      print_type: 1, //发票类型：1-普通发票，2-专用发票
      is_company: true, //抬头类型：true-企业，false-个人，个人就传姓名和身份证号信息，企业就传企业相关字段
      person_name: "", //个人姓名
      id_card: "", //身份证号
      company_name: "", //公司名称
      license_no: "", //营业执照编号
      mobile: "", //公司电话
      bank: "", //开户银行名称
      bank_no: "", //开户银行账号
      address: "", //公司开票地址
      note: "", //备注
    },
    isDialog: false,
  },
  change(event) {
    const {
      currentTarget: {
        dataset: { type },
      },
    } = event;
    let apply = JSON.parse(JSON.stringify(this.data.apply));
    apply[type] = event.detail.value;
    this.setData({
      apply,
    });
  },
  changeQy(event) {
    let val = event.detail.value;
    if (val == 1) {
      this.setData({
        "apply.is_company": true,
      });
    } else {
      this.setData({
        "apply.is_company": false,
      });
    }
  },
  changefp(event) {
    let val = event.detail.value;
    if (val == 1) {
      this.setData({
        "apply.print_type": 1,
      });
    } else {
      this.setData({
        "apply.print_type": 2,
      });
    }
  },
  confirm() {
    if (this.data.apply.company_name == "") {
      wx.showToast({
        title: "请输入公司名称",
        icon: "none",
        duration: 2000,
        mask: true,
      })
      return ;
    }
    if (this.data.apply.license_no == "") {
      wx.showToast({
        title: "请输入税号",
        icon: "none",
        duration: 2000,
        mask: true,
      })
      return ;
    }
    mineApi.invoiceApply(this.data.apply).then(res => {
      if(res.err_code != 0) {
        wx.showToast({
          title: res.err_msg,
          icon: 'none',
          duration: 2000,
          mask: true,
        })
        return ;
      }
      wx.showToast({
        title: '申请成功',
        icon: 'none',
        duration: 2000,
        mask: true,
      }, () => {
        wx.navigateBack({
          delta: 1,
        })
      });
    })
    // this.setData(
    //   {
    //     isDialog: true,
    //   },
    //   () => {
    //     setTimeout(() => {
    //       wx.navigateTo({
    //         url: "/pages/mine/spendingRecords/prePay/prePay",
    //       });
    //     }, 3000);
    //   }
    // );
  },
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on("apply", (data) => {
      let obj = data.data;
      console.log('obj === ', obj)
      const info = JSON.parse(JSON.stringify(this.data.apply));
      info.order_id = obj.id;
      this.setData({
        apply: info,
        title: obj.package_name,
        money: obj.pay_amount,
      });
    });
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onShareAppMessage() {
    return {
      title: "",
    };
  },
});
